import os
from PIL import Image  
  
# crop to 4 img 
def crop_4_fisheye(image_path, output_path,filename, r, delta):  
    image = Image.open(image_path)  
    
    valid_image = crop_circle_fisheye(image,r)
    imgwidth, imgheight = valid_image.size  
    box = (0, 0, imgwidth / 2 + delta, imgheight / 2 + delta)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(0, 0))  

    box = (imgwidth / 2 - delta, 0, imgwidth, imgheight/2+delta)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(imgwidth / 2 - delta, 0))

    box = (0, imgheight / 2 - delta, imgwidth / 2 + delta, imgheight)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(0, imgheight / 2 - delta))

    box = (imgwidth / 2 - delta, imgheight / 2 - delta, imgwidth, imgheight)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(imgwidth / 2 - delta, imgheight / 2 - delta))

# crop to 4 img
def crop_4(image_path, output_path,filename, delta):  
    valid_image = Image.open(image_path)  
    imgwidth, imgheight = valid_image.size  
    box = (0, 0, imgwidth / 2 + delta, imgheight / 2 + delta)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(0, 0))  

    box = (imgwidth / 2 - delta, 0, imgwidth, imgheight/2+delta)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(imgwidth / 2 - delta, 0))

    box = (0, imgheight / 2 - delta, imgwidth / 2 + delta, imgheight)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(0, imgheight / 2 - delta))

    box = (imgwidth / 2 - delta, imgheight / 2 - delta, imgwidth, imgheight)  
    cropped_image = valid_image.crop(box)  
    cropped_image.save(output_path + filename + '_{}_{}.jpg'.format(imgwidth / 2 - delta, imgheight / 2 - delta))

# crop circle in the img center from fisheye
def crop_circle_fisheye(image, r):
    imgwidth, imgheight = image.size
    box = ((imgwidth-2*r)/2,0,imgwidth/2+r,imgheight)  
    return image.crop(box)


  
image_path = '/home/unionsys/Pictures/crop/'  # 输入你的图片路径  
output_path = '/home/unionsys/Pictures/cropres/'  # 输入你的输出路径  

for file in os.listdir(path=image_path):
    crop_4_fisheye(image_path+file, output_path,file, 600, 100)


